(function(window){
	/*
	This class is dependent upon TweenMax
	*/
	if(!window.console) window.console = {log:function(p){}, info:function(){}, warn:function(){}};
		
	var UIElements = new function(){
		
		TweenPlugin.activate([CSSPlugin]);

		UIElements.prototype = new Object();
		UIElements.prototype.constructor = UIElements;

		var container;
		var rollOverBox;
		var lastId = "";
		var boxIsVisible = false;
		var starsView;
		var clickRadius = 30;

		function UIElements(pContainer, pStarsView){ 
			container = pContainer;
			starsView = pStarsView;
			rollOverBox = this.createRollOverBox();
			TweenLite.to(rollOverBox, 0, {css:{autoAlpha:0}, ease:Sine.easeInOut});

			var sky = document.getElementById("sky");
			var skyClick = document.createElement('DIV');
			skyClick.id = "skyClick";
			//sky.appendChild(skyClick);
		}	
		UIElements.prototype.toString = function(){
			return "[object UIElements]";
		}
		//create rollover graphic
		
		
		UIElements.prototype.createRollOverBox = function(){
			var newdiv = document.createElement('div');
			document.getElementById('sky').appendChild(newdiv);
			newdiv.setAttribute('id', 'rolloverbox');
			return newdiv;
		}
		
		//rollover listener	
		
		//show rollover box
		function setRolloverBoxText(cb){
			$(rollOverBox).html("<span class='rolloverBoxHeading'>"+cb.name+
				"</span><br><span class='rolloverBoxDetails'>Right ascension: </span><span class='rolloverBoxNumber'>"+NumberUtils.fixedLengthDecimal(cb.RA, 3)+
				"</span><br><span class='rolloverBoxDetails'>Declination: </span><span class='rolloverBoxNumber'>"+NumberUtils.fixedLengthDecimal(cb.Dec, 3)+
				"</span><br><span class='rolloverBoxDetails'>Altitude: </span><span class='rolloverBoxNumber'>"+NumberUtils.fixedLengthDecimal(cb.altitude, 3)+
				"</span><br><span class='rolloverBoxDetails'>Azimuth: </span><span class='rolloverBoxNumber'>"+NumberUtils.fixedLengthDecimal(cb.azimuth, 3)+"</span>")
		}
		function findNearest(point){
			var i;
			var nearest;
			var dist = 1e10;
			var allObjects = SolarSystem.elements.concat(StarsView.starArray)
			for(i = 0; i < allObjects.length; i++){
				if(allObjects[i].name != "bg" && allObjects[i].name != "earth"){
					var tempDist = Point.distance(new Point(allObjects[i].x, allObjects[i].y), point)
					if(tempDist < dist) {
						dist = tempDist;
						nearest = allObjects[i];
					}
				}
			}
			return {object:nearest, distance:dist};
		}
		UIElements.prototype.celestialBodyOver = function(event){
			var objectId = event.target.id;
			var objectParent = event.target.offsetParent;
			//console.log($(event.target.offsetParent).css('width'))
			// var nr = findNearest(new Point(
			// 	(parseInt(event.target.style.left)+event.offsetX)-(parseInt($(event.target.offsetParent).css('width'))/2), 
			// 	(parseInt(event.target.style.top)+event.offsetY)-(parseInt($(event.target.offsetParent).css('height'))/2))
			// )
			var no = findNearest(new Point(
				event.offsetX-(parseInt($(event.target).css('width'))/2), 
				event.offsetY-(parseInt($(event.target).css('height'))/2)
				)
			);
			if(no.distance > clickRadius) {
				//rollOverBox.style.visibility = 'hidden';
				UIElements.hideBox()
				//lastId = "";
				return;
			}			
			UIElements.hideBox();
			//if(objectId != 'solar_system' && objectId != 'rolloverbox' && objectId != 'stars'){
				// var body;
				// switch(nr.type){
				// 	case "planet" :
				// 		var body = SolarSystem.getCanvasElement(objectId);
						
				// 	break;
				// 	case "star" :
				// 		var body = StarsView.getStar(objectId);
				// 	break;
				// }
				setRolloverBoxText(no.object)
				positionOverBox(no.object);
				
				//rollOverBox.style.visibility = 'visible';
				UIElements.showBox()
			//} else {
				//hideBox();
			//}
			lastId = objectId;
		}
		// UIElements.prototype.solarBodyOver = function(event){
		// 	var objectId = event.target.id;
		// 	if(lastId == objectId && boxIsVisible) {
		// 		//rollOverBox.style.visibility = 'hidden';
		// 		hideBox()
		// 		lastId = "";
		// 		return;
		// 	}			
		// 	hideBox();
		// 	if(objectId != 'solar_system' && objectId != 'rolloverbox' && objectId != 'stars'){

		// 		var  planetDiv = event.target;
		// 		var planet = SolarSystem.getCanvasElement(objectId);
		// 		setRolloverBoxText(planet)
		// 		positionOverBox(planet);
				
		// 		//rollOverBox.style.visibility = 'visible';
		// 		showBox()
		// 	} else {
		// 		hideBox();
		// 	}
		// 	lastId = objectId;
		// }
		// UIElements.prototype.solarBodyOut = function(event){
		// 	var objectId = event.target.id;
		// 	if(objectId != 'solar_system' && objectId != 'rolloverbox' && objectId != 'stars'){
		// 		//rollOverBox.style.visibility = 'hidden';
		// 		hideBox()
		// 	}
		// }
		// UIElements.prototype.starOver = function(event){
		// 	var objectId = event.target.id;
		// 	if(lastId == objectId && boxIsVisible) {
		// 		//rollOverBox.style.visibility = 'hidden';
		// 		hideBox();
		// 		lastId = "";
		// 		return;
		// 	}

		// 	hideBox();
		// 	if(objectId != 'solar_system' && objectId != 'rolloverbox' && objectId != 'stars'){
		// 		var  planetDiv = event.target;
		// 		console.log(event.target)
		// 		var star = starsView.getStar(objectId);
		// 		console.log(starsView)
		// 		setRolloverBoxText(star)
		// 		positionOverBox(star);
		
		// 		//rollOverBox.style.visibility = 'visible';
		// 		showBox()
		// 	} else {
		// 		hideBox();
		// 	}
		// 	lastId = objectId;
		// }

		function positionOverBox(element){
			//console.log(element.x)
			var xPos = element.x+SolarSystem.canvas.width/2;
			var yPos = element.y+SolarSystem.canvas.height/2;
			var boxSize = UIElements.getElementTotalDimensions(rollOverBox);
			if(xPos+boxSize.width > SolarSystem.canvas.width) xPos -= boxSize.width;
			if(yPos+boxSize.height > SolarSystem.canvas.height) yPos -= boxSize.height;
			//rollOverBox.style.top = yPos;
			//rollOverBox.style.left = xPos;
			TweenLite.to(rollOverBox, 0.25, {css:{top:yPos}, ease:Sine.easeInOut});
			TweenLite.to(rollOverBox, 0.125, {css:{left:xPos}, ease:Sine.easeInOut});
		}
		UIElements.showBox = function(){
			//boxIsVisible = true;
			UIElements.showElement(rollOverBox)
			//TweenLite.to(rollOverBox, 0.25, {css:{autoAlpha:1}, ease:Sine.easeInOut});
		}
		UIElements.hideBox = function(){
			//boxIsVisible = false;
			UIElements.hideElement(rollOverBox)
			//TweenLite.to(rollOverBox, 0.25, {css:{autoAlpha:0}, ease:Sine.easeInOut});
		}
		UIElements.showElement = function(target, time, delay, onComplete){
			if(!TweenMax){
				console.warn("TweenMax is required");
				return;
			}
			if(!time) time = 0.5;
			if(!delay) delay = 0.0;
			TweenMax.to(target, time, {css:{autoAlpha:1}, delay:delay, ease:Sine.easeInOut})
		}
		UIElements.hideElement = function(target, time, delay, onComplete){
			if(!TweenMax){
				console.warn("TweenMax is required");
				return;
			}
			if(!time) time = 0.25;
			if(!delay) delay = 0.0;
			TweenMax.to(target, time, {css:{autoAlpha:0}, delay:delay, ease:Sine.easeInOut})
		}
		UIElements.getElementTotalDimensions = function(element){
			var el = $(element)
			var o = {}
			o.width = el.width()
				+getInt(el.css('marginLeft'))
				+getInt(el.css('marginRight'))
				+getInt(el.css('borderLeftWidth'))
				+getInt(el.css('borderRightWidth'))
				+getInt(el.css('paddingLeft'))
				+getInt(el.css('paddingRight'));
			o.height = $(el).height()
				+getInt(el.css('marginTop'))
				+getInt(el.css('marginBottom'))
				+getInt(el.css('borderTopWidth'))
				+getInt(el.css('borderBottomWidth'))
				+getInt(el.css('paddingTop'))
				+getInt(el.css('paddingBottom'));
			return o;
		}
		UIElements.getChildByName = function(parent, name, recurse){
			var a = [];
			function f(p, r){
				var c = p.children;
				for (var i = 0; i<c.length; i++) {
					if(c[i].nodeName.toUpperCase() == name.toUpperCase()){
						a.push(c[i]);
					}
					if(c[i].nodeType == 1 && recurse == true){
						f(c[i], r);
					}
				};
			}
			f(parent, recurse);
			return a.length > 0 ? a : null;
		}
		UIElements.getChildById = function(parent, id, recurse){
			var a = [];
			function f(p, r){
				var c = p.children;
				for (var i = 0; i<c.length; i++) {
					if(c[i].id == id){
						a.push(c[i]);
					}
					if(c[i].nodeType == 1 && recurse == true){
						f(c[i], r);
					}
				};
			}
			f(parent, recurse);
			return a.length > 0 ? a : null;
		}

		UIElements.prototype.getContainer = function(){
			return container;
		}
		function getInt(value){
			return !value ? 0 : parseInt(value);
		}

		return UIElements;
	}
	if(!window.UIElements) window.UIElements = UIElements;
}(window));